{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/omkenge/cookbook/blob/add-new-example-for-prompting/examples/prompting/Zero_shot_prompting.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Hfb0zeHYnwtC"
      },
      "source": [
        "##### Copyright 2025 Google LLC."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "cellView": "form",
        "id": "NeImViGqnv1b"
      },
      "outputs": [],
      "source": [
        "# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "sP8PQnz1QrcF"
      },
      "source": [
        "# Gemini API: Zero-shot prompting"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "bxGr_x3MRA0z"
      },
      "source": [
        "<a target=\"_blank\" href=\"https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/prompting/Zero_shot_prompting.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" height=30/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ysy--KfNRrCq"
      },
      "source": [
        "You can use the Gemini models to answer many queries without any additional context. Zero-shot prompting is useful for situations when your queries are not complicated and do not require a specific schema."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "id": "Ne-3gnXqR0hI"
      },
      "outputs": [],
      "source": [
        "%pip install -U -q \"google-genai>=1.0.0\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "eomJzCa6lb90"
      },
      "source": [
        "## Configure your API key\n",
        "\n",
        "To run the following cell, your API key must be stored it in a Colab Secret named `GOOGLE_API_KEY`. If you don't already have an API key, or you're not sure how to create a Colab Secret, see [Authentication](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Authentication.ipynb) for an example."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "id": "v-JZzORUpVR2"
      },
      "outputs": [],
      "source": [
        "from google import genai\n",
        "from google.colab import userdata\n",
        "\n",
        "GOOGLE_API_KEY = userdata.get('GOOGLE_API_KEY')\n",
        "client = genai.Client(api_key=GOOGLE_API_KEY)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GLh4oXd1VXTw"
      },
      "source": [
        "## Examples\n",
        "\n",
        "Here are a few examples with zero-shot prompting. Note that in each of these examples, you can simply provide the task, with zero examples."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "id": "Ym4w9z3iWHlT"
      },
      "outputs": [],
      "source": [
        "MODEL_ID=\"gemini-2.5-flash\" # @param [\"gemini-2.5-flash-lite\",\"gemini-2.5-flash\",\"gemini-2.5-pro\",\"gemini-3-pro-preview\"] {\"allow-input\":true, isTemplate: true}"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "id": "J580DkQPVYYp"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": "Here's the order of the animals from biggest to smallest:\n\n1.  **Elephant**\n2.  **Dog**\n3.  **Fish**",
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "execution_count": 6,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from IPython.display import Markdown",
        "\n",
        "prompt = \"\"\"\n",
        "    Sort following animals from biggest to smallest:\n",
        "    fish, elephant, dog\n",
        "\"\"\"\n",
        "\n",
        "response = client.models.generate_content(\n",
        "    model=MODEL_ID,\n",
        "    contents=prompt,\n",
        ")\n",
        "\n",
        "Markdown(response.text)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "id": "K1we-_q4VZ0M"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": "Positive\n",
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "execution_count": 7,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "prompt = \"\"\"\n",
        "    Classify sentiment of review as positive, negative or neutral:\n",
        "    I go to this restaurant every week, I love it so much.\n",
        "\"\"\"\n",
        "\n",
        "response = client.models.generate_content(\n",
        "    model=MODEL_ID,\n",
        "    contents=prompt,\n",
        ")\n",
        "\n",
        "Markdown(response.text)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "id": "4NF2OmfPVa4l"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": "Rome and Berlin\n",
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "execution_count": 8,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "prompt = \"\"\"\n",
        "    Extract capital cities from the text:\n",
        "    During the summer I visited many countries in Europe. First I visited Italy, specifically Sicily and Rome.\n",
        "    Then I visited Cologne in Germany and the trip ended in Berlin.\n",
        "\"\"\"\n",
        "\n",
        "response = client.models.generate_content(\n",
        "    model=MODEL_ID,\n",
        "    contents=prompt,\n",
        ")\n",
        "\n",
        "Markdown(response.text)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "metadata": {
        "id": "N-6Bz_J9R-pL"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": "```python\ndef add_numbers(a, b):\n    return a + b\n\nprint(add_numbers(5, 10))\n```\n\n**Error:**\n\nThe error is that the code is trying to add an integer (5) to a string (\"10\").  Python doesn't allow direct addition between integers and strings without explicit type conversion. This causes a `TypeError`.\n\n**Fix:**\n\nTo fix this, you can either:\n\n1. **Convert the string \"10\" to an integer:**  Change `print(add_numbers(5, \"10\"))` to `print(add_numbers(5, int(\"10\")))`\n\n   *OR*\n\n2. **Pass the second argument as an integer directly:** Change `print(add_numbers(5, \"10\"))` to `print(add_numbers(5, 10))` (as shown in the corrected code above).\n\nThe corrected code shown above takes the second approach, assuming the intention was to add the numbers 5 and 10. This is generally the preferred solution if you can control the input.  If you're getting input from a source that might provide a string, the first approach (using `int()`) might be more robust.\n",
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "prompt = \"\"\"\n",
        "    Find and fix the error in this Python code:\n",
        "    def add_numbers(a, b):\n",
        "        return a + b\n",
        "    print(add_numbers(5, \"10\"))\n",
        "\"\"\"\n",
        "\n",
        "response = client.models.generate_content(\n",
        "    model=MODEL_ID,\n",
        "    contents=prompt,\n",
        ")\n",
        "\n",
        "Markdown(response.text)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {
        "id": "cg1vehMqSKVR"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": "Average speed is calculated by dividing the total distance traveled by the total time taken.\n\n*   **Distance:** 120 km\n*   **Time:** 2 hours\n\nAverage speed = Distance / Time = 120 km / 2 hours = 60 km/h\n\nSo, the train's average speed is $\\boxed{60}$ km/h.\n",
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "prompt = \"\"\"\n",
        "    Solve this math problem:\n",
        "    A train travels 120 km in 2 hours. What is its average speed?\n",
        "\"\"\"\n",
        "\n",
        "response = client.models.generate_content(\n",
        "    model=MODEL_ID,\n",
        "    contents=prompt,\n",
        ")\n",
        "\n",
        "Markdown(response.text)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {
        "id": "NSEXgLKaSj4x"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": "Here's the breakdown of the names in the text:\n\n*   **People:** Emmanuel Macron\n*   **Countries:**  France, United Arab Emirates\n",
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "prompt = \"\"\"\n",
        "    Identify the names of people, places, and countries in this text:\n",
        "    Emmanuel Macron, the president of France, announced a AI partnership in collaboration with the United Arab Emirates.\n",
        "\"\"\"\n",
        "\n",
        "response = client.models.generate_content(\n",
        "    model=MODEL_ID,\n",
        "    contents=prompt,\n",
        ")\n",
        "\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "metadata": {
        "id": "emQf-5XtSrcI"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": "Here are a few options for correcting the grammar, depending on the intended meaning:\n\n*   **She doesn't like playing football, but she enjoys watching it.** (This is the most common and likely correct version, fixing the subject-verb agreement and using the correct verb form after \"enjoy.\")\n\n*   **She doesn't like playing football, but she enjoys to watch.** (While grammatically correct, using \"to watch\" after enjoy is uncommon. Usually \"watching\" is preferred.)\n\n**Explanation of the Errors:**\n\n*   **\"She don't like\"** is incorrect because the third-person singular pronoun \"she\" requires the verb \"doesn't\" (does not).\n*   **\"she enjoy to watch it\"** is incorrect. After the verb \"enjoy,\" we generally use the gerund form (verb + -ing) or \"watching.\" While \"to watch\" is grammatically correct it is not commonly used.\n",
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "prompt = \"\"\"\n",
        "    Correct the grammar in this sentence:\n",
        "    She don't like playing football but she enjoy to watch it.\n",
        "\"\"\"\n",
        "\n",
        "response = client.models.generate_content(\n",
        "    model=MODEL_ID,\n",
        "    contents=prompt,\n",
        ")\n",
        "\n",
        "display(Markdown(response.text))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "_TghYaiGobM5"
      },
      "source": [
        "## Next steps\n",
        "\n",
        "Be sure to explore other examples of prompting in the repository. Try writing prompts about classifying your own data, or try some of the other prompting techniques such as [few-shot prompting](https://github.com/google-gemini/cookbook/blob/main/examples/prompting/Few_shot_prompting.ipynb)."
      ]
    }
  ],
  "metadata": {
    "colab": {
      "name": "Zero_shot_prompting.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
